package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.PhysicalCardChangeLogEmEntity;
import city.spring.modules.cardinfo.service.PhysicalCardChangeLogEmService;
import city.spring.modules.cardinfo.vo.PhysicalCardChangeLogEmEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：员工物理卡号更换记录
 *
 * @author pxm
 * @date 2020-12-25 11:53:52
 */
@RestController
@RequestMapping("/physical_card_change_log_em")
public class PhysicalCardChangeLogEmController extends ApiController {
    private final PhysicalCardChangeLogEmService physicalCardChangeLogEmService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<PhysicalCardChangeLogEmEntity, ?>> entityOrderFields;

    public PhysicalCardChangeLogEmController(PhysicalCardChangeLogEmService physicalCardChangeLogEmService) {
        this.physicalCardChangeLogEmService = physicalCardChangeLogEmService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getId);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getPersonId);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getPersonType);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getOldCard);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getNewCard);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getReplaceTime);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getGmtCreate);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getGmtModified);
        entityOrderFields.add(PhysicalCardChangeLogEmEntity::getCounts);
    }

    /**
     * 获取全部的 <strong>员工物理卡号更换记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, PhysicalCardChangeLogEmEntity entity) {
        LambdaQueryChainWrapper<PhysicalCardChangeLogEmEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>员工物理卡号更换记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, PhysicalCardChangeLogEmEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEmEntity> queryWrapper = buildQuery(entity);
        return success(physicalCardChangeLogEmService.staffListPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>员工物理卡号更换记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<PhysicalCardChangeLogEmEntityVO> page = new Page<>();
        QueryWrapper<PhysicalCardChangeLogEmEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<PhysicalCardChangeLogEmEntityVO> iPage = physicalCardChangeLogEmService.staffListPage(page, wrapper);
        List<PhysicalCardChangeLogEmEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>员工物理卡号更换记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody PhysicalCardChangeLogEmEntity entity) {
        physicalCardChangeLogEmService.savePhysicalCardChangeLogEm(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>员工物理卡号更换记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody PhysicalCardChangeLogEmEntity entity) {
        entity.setId(id);
        physicalCardChangeLogEmService.updatePhysicalCardChangeLogEm(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>员工物理卡号更换记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        physicalCardChangeLogEmService.deletePhysicalCardChangeLogEm(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>员工物理卡号更换记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        physicalCardChangeLogEmService.deletePhysicalCardChangeLogEm(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<PhysicalCardChangeLogEmEntity> buildLambdaQuery(PhysicalCardChangeLogEmEntity entity) {
        LambdaQueryChainWrapper<PhysicalCardChangeLogEmEntity> lambdaQuery = physicalCardChangeLogEmService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), PhysicalCardChangeLogEmEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonId()), PhysicalCardChangeLogEmEntity::getPersonId, entity.getPersonId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonType()), PhysicalCardChangeLogEmEntity::getPersonType, entity.getPersonType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldCard()), PhysicalCardChangeLogEmEntity::getOldCard, entity.getOldCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getNewCard()), PhysicalCardChangeLogEmEntity::getNewCard, entity.getNewCard());
        lambdaQuery.eq(entity.getReplaceTime() != null, PhysicalCardChangeLogEmEntity::getReplaceTime, entity.getReplaceTime());
        lambdaQuery.eq(entity.getGmtCreate() != null, PhysicalCardChangeLogEmEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, PhysicalCardChangeLogEmEntity::getGmtModified, entity.getGmtModified());
        lambdaQuery.eq(entity.getCounts() != null, PhysicalCardChangeLogEmEntity::getCounts, entity.getCounts());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends PhysicalCardChangeLogEmEntity> buildQuery(PhysicalCardChangeLogEmEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonId()), "t1.person_id", entity.getPersonId());
        wrapper.like(StringUtils.isNotBlank(entity.getStaffName()), "t2.name", entity.getStaffName());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonType()), "t1.person_type", entity.getPersonType());
        wrapper.like(StringUtils.isNotBlank(entity.getOldCard()), "t1.old_card", entity.getOldCard());
        wrapper.like(StringUtils.isNotBlank(entity.getNewCard()), "t1.new_card", entity.getNewCard());
        wrapper.eq(entity.getReplaceTime() != null, "t1.replace_time", entity.getReplaceTime());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        wrapper.eq(entity.getCounts() != null, "t1.counts", entity.getCounts());
        return wrapper;
    }

}
